import Paging from "../../../../wxjs/Paging"
var pag = new Paging();
var app = getApp()

Page(Object.assign(pag.func(), {
  data: {
    isPullDown: false, // 是否触发下拉刷新
    babyList: [],
    carefulUserList: [],
    aliveCount: 0,
    form: {
      page: 1,
      rows: 10
    }
  },
  listParams: {},
  reqUrl:app.serAddrs['getCarefulUserList'],
  header: {'content-type': 'application/x-www-form-urlencoded'},
  onLoad(){
    this.login()
  },
  callBack(res,lists) {
    this.setData({
      lists: this.handleBabyList(res.data.list)
    })
  },
  login(){
    var _this = this
    app.xlogin.getUser(function(res) {
      var myInfo = app.isNavigateToLogin();
      if (!myInfo) return;
  
      myInfo = app.isNavigateToBind();
      if (!myInfo) return;

      _this.pageListInit()
    })
  },
  //获取关心的用户列表
  getCareFulUserList(){
    var that = this
    app.middleware.requestAjax({
      serAddr: app.serAddrs['getCarefulUserList'],
      data: this.data.form,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if(that.data.isPullDown){
          wx.stopPullDownRefresh()
        }
        that.setData({
          carefulUserList: that.handleBabyList(res.data.list),
          isPullDown: false
        })
      }
    })    
  },
  // 处理宝贝列表
  handleBabyList(list){
    var bgs = ['orange', 'purple', 'blue']
    var i = 0
    var babyList = list.map(item => {
      item.bg = bgs[i]
      item.gender = item.sex == 0 ? 'male' : 'female'
      item.lastTrainingTime = this.handleLastTrainingTime(item.lastTrainingTime)
      if (i===2) {
        i = 0
      }
      i++
      return item
    })
    return babyList
  },
  // 处理最后一次运动时间
  handleLastTrainingTime(time){
    var timeText = ''
    if (parseInt(time/1000/60) < 1) {
      timeText = '刚刚'
    } else if (parseInt(time/1000/60) >= 1 && parseInt(time/1000/60) < 60) {
      timeText = parseInt(time/1000/60) + '分钟前'
    } else if (parseInt(time/1000/60) >= 60&&parseInt(time/1000/60/60) < 24) {
      timeText = parseInt(time/1000/60/60) + '小时前'
    } else if (parseInt(time/1000/60/60)>=24&&parseInt(time/1000/60/60/24) < 30){
      timeText = parseInt(time/1000/60/60/24) + '天前'
    } else if (parseInt(time/1000/60/60/24)>=30&&parseInt(time/1000/60/60/24/30) < 12){
      timeText = parseInt(time/1000/60/60/24/30) + '个月前'
    } else if (parseInt(time/1000/60/60/24/30/12)>=1){
      timeText = parseInt(time/1000/60/60/24/30/12) + '年前'
    } else {
      timeText = '日期不详'
    }
    return timeText
  },
  // 点击宝贝卡片
  tapBaby(e){
    var list
    var editable
    if (this.data.currentPage === 'mine') {
      list = this.data.babyList
      editable = true
    } else {
      list = this.data.carefulUserList
      editable = false
    }
    var current = list[e.currentTarget.dataset.index]
    // 全局變量
    current.guardianUserId = app.userInfo.userId
    app.currentChild = current

    wx.navigateTo({
      url: app.xhx_route.babyDetail+`?childName=${current.nickname}&childId=${current.childId}&currentPage=`+this.data.currentPage,
    })
  },
  // 切换自定义导航栏
  changeNavigation(e) {
    var detail = e.detail
    if (detail == 'care' && !this.data.carefulUserList.length) {
      this.getCareFulUserList()
    } else if (detail == 'mine' && !this.data.babyList.length) {
      this.getBabyList()
    }
    this.setData({
      currentPage: detail
    })
  },
  // 点击右下方固定图标
  tapIcon(){
    if (this.data.currentPage === 'mine') {
      wx.navigateTo({
        url: app.xhx_route.babyMessageList
      })
    }
  },
  // 点击发布按钮
  tapPublishIcon(e) {
    this.setData({
      pageShadowStatus: e.detail === 'show'
    })
  },
  onPullDownRefresh(){
    this.setData({
      isPullDown: true
    })
    if (this.data.currentPage==='mine'){
      this.getBabyList()
    }else{
      this.getCareFulUserList()
    }
  }
}))
